package project.Servlet;

import org.json.JSONObject;
import project.Dao.MessageDao;
import project.Dao.UserDao;
import project.Entity.Message;
import project.Entity.User;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;

public class action extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  {
        System.out.println("执行了doGet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response){
        System.out.println("执行了doPost");
    }

    protected void service(HttpServletRequest request, HttpServletResponse response){
        System.out.println("执行了login");
        try {
            process(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void process(HttpServletRequest request, HttpServletResponse response) throws Exception{

        String email = request.getParameter("email");
        String action = request.getParameter("action");
        request.setCharacterEncoding("UTF-8");
        out.println("注册邮箱:" + email + "\n指令:" + action);
        JSONObject json = new JSONObject();

        switch (action){
            case "get_friends":
            {
                UserDao userDao = new UserDao();
                List<User> list = userDao.getUserByEmail("all");
                if(list.size()==0){
                    json.put("result", 0);
                    json.put("reason", "未获取到好友信息！");
                }
                else {
                    List jsonList = new ArrayList();
                    for(int i = 0; i < list.size(); i ++){
                        Map map = new HashMap();
                        map.put("email",list.get(i).getEmail());
                        map.put("name",list.get(i).getName());
                        map.put("url",list.get(i).getUrl());
                        map.put("time",list.get(i).getTime());
                        jsonList.add(map);
                    }
                    json.put("aaData", jsonList);
                    json.put("result", 1);
                }
                break;
            }
            case "get_my_record":
            {
                String toEmail = request.getParameter("toEmail");
                MessageDao messageDao = new MessageDao();
                List<Message> list= messageDao.queryRecord(email, toEmail);
                if(list.size()==0){
                    json.put("result", 0);
                    json.put("reason", "没有查询到24小时内的聊天记录");
                    break;
                }
                List jsonList = new ArrayList();
                for(int i = 0; i < list.size(); i ++){
                    Map map = new HashMap();
                    map.put("id",list.get(i).id);
                    map.put("host",list.get(i).host);
                    map.put("toWho",list.get(i).toWho);
                    map.put("content",list.get(i).content);
                    map.put("time",list.get(i).time);
                    jsonList.add(map);
                }
                json.put("aaData", jsonList);
                json.put("result", 1);
                break;
            }
            case "send_message":
            {
                String toEmail = request.getParameter("toEmail");
                String content = request.getParameter("content");
                UserDao userDao = new UserDao();
                List<User> list1 = userDao.getUserByEmail(email);
                if(list1.size()==0){
                    json.put("result", 0);
                    json.put("reason", "请先注册一个账号！");
                    break;
                }
                List<User> list = userDao.getUserByEmail(toEmail);
                if(list.size()==0){
                    json.put("result", 0);
                    json.put("reason", "未找到目标！");
                    break;
                }
                MessageDao messageDao = new MessageDao();
                Message message = new Message(1,email,toEmail,content,"now");
                if(messageDao.saveMessage(message)){
                    json.put("result", 1);
                    break;
                }
                json.put("result", 0);
                json.put("reason", "发送信息失败！仅支持文字消息");
                break;
            }

            case "delete_record":
            {
                String toEmail = request.getParameter("toEmail");
                out.println("目的邮箱："+toEmail);
                UserDao userDao = new UserDao();
                List<User> list1 = userDao.getUserByEmail(email);
                if(list1.size()==0){
                    json.put("result", 0);
                    json.put("reason", "请先注册一个账号！");
                    break;
                }
                List<User> list = userDao.getUserByEmail(toEmail);
                if(list.size()==0){
                    json.put("result", 0);
                    json.put("reason", "未找到目标！");
                    break;
                }
                MessageDao messageDao = new MessageDao();
                if(messageDao.deleteMessage(email, toEmail)){
                    json.put("result", 1);
                    break;
                }
                json.put("result", 0);
                json.put("reason", "未查询到记录！");
                break;
            }
        }


        out.println(json);
        //将响应消息返回页面
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().print(json);
        response.getWriter().flush();
        response.getWriter().close();


    }
}
